package com.java.tutorials.apache.kafka.springIntegration;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

import java.util.Random;
/**
 * Created by xuzhike on 2016/2/28.
 */
public class Producer {
    private static final String CONFIG = "classpath:apache/kafka/Producer_context.xml";
    private static Random rand = new Random();

    public static void main(String[] args) {
//        new FileSystemXmlApplicationContext("classpath:appcontext.xml");
//        final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Producer_context.xml", Producer.class);
//        ctx.start();

        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load(CONFIG);
        ctx.refresh();

        final MessageChannel channel = ctx.getBean("inputToKafka", MessageChannel.class);

        for (int i = 0; i < 100; i++) {
            channel.send(MessageBuilder.withPayload("Message-" + rand.nextInt()).setHeader("messageKey", String.valueOf(i)).setHeader("topic", "test").build());
        }

        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ctx.close();
    }
}
